package deadlocktest;

public class Main {

    public static void main(String[] args) {

        //搞两个对象作为锁

        Object o1 = new Object();
        Object o2 = new Object();

        new Thread(){
            @Override
            public void run() {
                synchronized (o1){
                    System.out.println("线程1获取了o1锁");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (o2){
                        System.out.println("线程1获取了o2锁");
                    }
                }
            }
        }.start();


        new Thread(){
            @Override
            public void run() {
                synchronized (o2){
                    System.out.println("线程2获取了o2锁");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (o1){
                        System.out.println("线程2获取了o1锁");
                    }
                }
            }
        }.start();







    }
}
